package com.example.kugou.controller;

import com.example.kugou.common.fileUpload.FileUploadTool;
import com.example.kugou.common.fileUpload.pojo.FileEntity;
import com.example.kugou.pojo.Mv;
import com.example.kugou.service.MvService;
import com.example.kugou.service.SingerService;
import com.example.kugou.vo.MvVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/mv")
@Api(tags = "MV相关接口", description = "提供MV相关的Rest API")
public class MvController {

    @Resource
    MvService mvService;

    @Resource
    SingerService singerService;

    public MvService getMvService() {
        return mvService;
    }

    public void setMvService(MvService mvService) {
        this.mvService = mvService;
    }


    @ApiOperation("根据编号查询所有mv")
    @RequestMapping("/selectBymvid")
    public MvVO selectBymvid(@RequestParam(value = "mvid", required = false) Integer mvid) {
        MvVO mvVO = new MvVO();
        List<Mv> mvs = mvService.showAllMV(mvid);
        if (mvs.size() > 0) {
            mvVO.setMsg("查询成功");
            mvVO.setStatus(true);
            mvVO.setData(mvs);
        } else {
            mvVO.setMsg("查询失败");
            mvVO.setStatus(false);
        }
        return mvVO;
    }


    @ApiOperation("根据编号删除mv")
    @RequestMapping("/removeMv")
    public MvVO deleteMv(@RequestParam(value = "vid", required = false) Integer vid) {
        MvVO mvVO = new MvVO();
        int delete = mvService.delete(vid);
        if (delete > 0) {
            mvVO.setMsg("删除成功");
            mvVO.setStatus(true);
        } else {
            mvVO.setMsg("删除失败");
            mvVO.setStatus(false);
        }
        return mvVO;
    }


    @ApiOperation("添加视频")
    @PostMapping("/insertmv")
    public MvVO insertmv(MultipartFile inputfile, MultipartFile inputsrcfile,
                         @RequestParam(value = "name", defaultValue = "") String name,
                         @RequestParam(value = "singerid", defaultValue = "0") int singerid,
                         @RequestParam(value = "singid", defaultValue = "0") int singid,
                         @RequestParam(value = "mvid", defaultValue = "0") int mvid,
                         HttpServletRequest request) {
        boolean isHave = false;
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("inputsrcfile");
        String srcName = inputsrcfile.getOriginalFilename();
        if (!file.isEmpty()) {
            isHave = true;
            System.out.println("非空");
        } else {
            isHave = false;
            System.out.println("空的");
        }
        FileEntity fileEntity = new FileUploadTool().createFile(isHave, inputfile, request);
        Mv mv = new Mv();
        fileEntity.setImagesPath(srcName);
        mv.setMpic("img/"+fileEntity.getImagesPath());
        mv.setVideo(fileEntity.getPath());
        System.out.println(singerid);
        System.out.println(singid);
        mv.setMvid(mvid);
        mv.setSingerid(singerid);
        mv.setSingid(singid);
        mv.setName(name);
        MvVO mvVO = new MvVO();
        int add = mvService.addMV(mv);
        if (add > 0) {
            mvVO.setMsg("添加成功");
            mvVO.setStatus(true);
        } else {
            mvVO.setMsg("添加失败");
            mvVO.setStatus(false);
        }
        return mvVO;
    }

    @ApiOperation("根据编号查询对应的mv的对象")
    @GetMapping("/selectmvById")
    public MvVO selectmvById(int id) {
        MvVO mvVO = new MvVO();
        Mv mv = mvService.selectmvById(id);
        if (mv !=null && mv.getMvid() > 0) {
            mvVO.setMsg("查询成功");
            mvVO.setStatus(true);
            mvVO.setMv(mv);
        } else {
            mvVO.setMsg("查询失败");
            mvVO.setStatus(false);
        }
        return mvVO;
    }




    @ApiOperation("修改mv")
    @RequestMapping("/updateMV")
    public MvVO updateMV(Mv mv) {
        MvVO mvVO = new MvVO();
        int update = mvService.updateMV(mv);
        if (update > 0) {
            mvVO.setMsg("修改成功");
            mvVO.setStatus(true);
        } else {
            mvVO.setMsg("修改失败");
            mvVO.setStatus(false);
        }
        return mvVO;
    }
}
